﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using LinqToDB;

namespace Winfrom_Login_Test_0001
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public static Dictionary<string, string> users = new Dictionary<string, string>();

        private void 登陆_Click(object sender, EventArgs e)
        {                     
            
            var uid = textBox1.Text;
            var pwd = textBox2.Text;   

            var usr = users.Where(x => x.Key == uid && x.Value == pwd)
                .Select(x => new { key = x.Key, value = x.Value }).FirstOrDefault();//返回新的匿名特性

            if (usr != null)
            {
                MessageBox.Show("登陆成功！");
                LoginSuccess loginSuccess = new LoginSuccess();
                this.Hide();
                loginSuccess.Show();
            }
            else
            {
                MessageBox.Show("账号或密码错误，请重新登陆或注册账号！");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Register register = new Register();
            register.Show();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            var conString = "server=.;database = Admin001;uid = sa;pwd = cultural0725.";

            SqlConnection sqlConnection = new SqlConnection(conString);

            string sql = "select * from Users";

            SqlCommand command = new SqlCommand(sql, sqlConnection);

            SqlDataAdapter dataAdapter = new SqlDataAdapter();

            DataTable table = new DataTable();

            sqlConnection.Open();

            dataAdapter.SelectCommand = command;

            dataAdapter.Fill(table);
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {            

        }

        private void Form1_Load_1(object sender, EventArgs e)
        {
            users.Add("Admin","123");
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            var name = textBox1.Text;

            var conString = "server=.;database = Admin001;uid = sa;pwd = cultural0725.";

            SqlConnection sqlConnection = new SqlConnection(conString);

            var dt = string.Format("select * from Users Where Username like '%%'", name);

            SqlCommand command = new SqlCommand(dt, sqlConnection);

            SqlDataAdapter dataAdapter = new SqlDataAdapter();

            dataAdapter.SelectCommand = command;

            DataTable table = new DataTable();

            sqlConnection.Open();

            dataAdapter.Fill(table);

        }
    }
}
